<template>
  <div>
    <el-dialog
      :title="titleText"
      :visible.sync="isVisible"
      width="40%"
      @close="onClose"
      @open="onOpen"
    >
    <el-form :model="addForm" :rules="addFormRules" ref="addFormRef"
    label-width="100px">
    <el-form-item :label="title " prop="attr_name">
      <el-input v-model="addForm.attr_name"></el-input>
    </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ConfigParams',
  data() {
    return {
      isVisible: false,
      paramsTitle: '',
      addForm: {
        attr_name: ''
      },
      addFormRules: {
        attr_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    show: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      required: true
    },
    cateId: {
      type: Number,
      required: true
    },
    tabName: {
      type: String,
      required: true
    },
    bindId: {
      type: Number,
      required: false
    },
    flag: {
      type: String,
      required: true
    }
  },
  watch: {
    show() {
      this.isVisible = this.show
    },
    title() {
      this.paramsTitle = '添加' + this.title
    }
  },
  computed: {
    titleText() {
      return '添加' + this.title
    }
  },
  methods: {
    onClose() {
      this.$emit('update:bindId', 0)
      this.$refs.addFormRef.resetFields()
      this.$emit('update:show', false)
    },
    onOpen() {
      console.log('on open')
      console.log('cateId:', this.cateId)
      console.log('bindId:', this.bindId)
      if (this.bindId > 0) {
        this.getParamData()
      }
    },
    save() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) { return false }
        if (this.flag === 'add') {
          console.log(this.tabName)
          const { data: res } = await this.$http.post(`categories/${this.cateId}/attributes`, {
            attr_name: this.addForm.attr_name,
            attr_sel: this.tabName
          })
          if (res.meta.status !== 201) {
            return this.$message.error(res.meta.msg)
          }
        } else {
          console.log('edit')
          const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${this.bindId}`, {
            attr_name: this.addForm.attr_name,
            attr_sel: this.tabName
          })
          if (res.meta.status !== 200) {
            return this.$message.error(res.meta.msg)
          }
          this.$message.success(res.meta.msg)
        }
        this.onClose()
        this.$emit('refresh')
      })
    },
    async getParamData() {
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes/${this.bindId}`, {
        params: { attr_sel: this.tabName }
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.addForm = res.data
      console.log(res.data)
    }
  }
}
</script>

<style scoped lang="less">

</style>
